import 'package:flutter/material.dart';
import 'package:fphonelocation/fphonelocation.dart';


void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver{

  double? latitude;
  double? longitude;

  @override
  void initState() {
    Fphonelocation.init(isSingle: false);
    Fphonelocation.setLocationCall(locationCall: (double latitude,double longitude){
      print('--->setLocationCall,latitude=$latitude,longitude=$longitude');
      this.latitude = latitude;
      this.longitude = longitude;
      setState(() {});
    });
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    Fphonelocation.stopLocation();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    switch(state){
      case AppLifecycleState.inactive:
      //inactive:用户可见，但不可响应用户操作
        break;
      case AppLifecycleState.paused:
      //paused:已经暂停了，用户不可见、不可操作
        Fphonelocation.stopLocation();
        break;
      case AppLifecycleState.resumed:
        //从后台返回到前台，重新赋值，表示需要司机从新选择任务
        Fphonelocation.startLocation();
        break;
      case AppLifecycleState.detached:
      //detached：当前页面即将退出
        break;
      case AppLifecycleState.hidden:
      // TODO: Handle this case.
        break;
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Text('原生传过来的经纬度为：${latitude},${longitude}'),
        ),
      ),
    );
  }
}
